import 'package:largefriends/entity/entities/db_country_entity.dart';

import '../entity/entities/db_state_entity.dart';
import '../utils/binary_util.dart';
import 'lf_read_sql.dart';

class LfLocationDbHelper {
  late final LfReadSql _sql = LfReadSql();

  LfLocationDbHelper._internal();

  static final LfLocationDbHelper _instance = LfLocationDbHelper._internal();

  static LfLocationDbHelper get instance => _instance;

  Future close() async {
    var result = _sql.close();
    return result;
  }

  Future<List<DbCountryEntity>> getCountryList(
      {bool needAnyWhere = false}) async {
    var dbClient = await _sql.db;
    var maps = await dbClient.query('countries', orderBy: 'weight DESC');
    var result =
        List.generate(maps.length, (index) => maps[index].toDbCountryEntity());
    if (needAnyWhere) {
      DbCountryEntity countryEntity = DbCountryEntity('0', "Anywhere");
      result.insert(0, countryEntity);
    }
    result.removeWhere((element){
      return  element.name.toUpperCase() == 'N/A';
    });
    return result;
  }

  Future<DbCountryEntity?> getCountryById(String id) async {
    DbCountryEntity? dbCountryEntity;
    var dbClient = await _sql.db;
    var maps = await dbClient
        .query('countries', where: '"cou_id"= ?', whereArgs: [id]);

    if (maps.isNotEmpty) {
      dbCountryEntity = maps.first.toDbCountryEntity();
    }

    return dbCountryEntity;
  }

  Future<DbCountryEntity?> getCountryByName(String name) async {
    DbCountryEntity? dbCountryEntity;
    var dbClient = await _sql.db;
    var maps = await dbClient
        .query('countries', where: '"cou_name"= ?', whereArgs: [name]);
    if (maps.isNotEmpty) {
      dbCountryEntity = maps.first.toDbCountryEntity();
    }
    return dbCountryEntity;
  }

  Future<List<DbStateEntity>> getStateListByCountryId(String countryId,
      {bool needAnyWhere = false}) async {
    var dbClient = await _sql.db;
    var maps = await dbClient
        .query('states', where: '"cou_id"= ?', whereArgs: [countryId]);

    var result = List.generate(
      maps.length,
      (index) => maps[index].toDbStateEntity(),
    );
    result.sort((a, b) => a.name.compareTo(b.name));
    if (needAnyWhere) {
      DbStateEntity stateEntity = DbStateEntity('0', "Anywhere", countryId);
      result.insert(0, stateEntity);
    }
    result.removeWhere((element) => element.name.toUpperCase() == 'N/A');
    if(countryId == '237'){
      result.removeWhere((element){
        return element.id == '4453' || element.id == '4454' || element.id == '4455' || element.id == '4456' || element.id == '4458' ||element.id == '4462';
      });
    }
    return result;
  }

  Future<DbStateEntity?> getStateById(String id) async {
    DbStateEntity? dbStateEntity;
    var dbClient = await _sql.db;
    var maps =
        await dbClient.query('states', where: '"stt_id"= ?', whereArgs: [id]);
    if (maps.isNotEmpty) {
      dbStateEntity = maps.first.toDbStateEntity();
    }
    return dbStateEntity;
  }

  Future<DbStateEntity?> getStateByName(String name) async {
    DbStateEntity? dbStateEntity;
    var dbClient = await _sql.db;
    var maps = await dbClient
        .query('states', where: '"stt_name"= ?', whereArgs: [name]);
    if (maps.isNotEmpty) {
      dbStateEntity = maps.first.toDbStateEntity();
    }
    return dbStateEntity;
  }
}

extension MapEx on Map<String, Object?> {
  DbCountryEntity toDbCountryEntity() {
    return DbCountryEntity(
        this['cou_id'].toString(), this['cou_name'].toString(),
        abbr: this['cou_abbr'].toString(),
        weight: parseInt(this['weight'].toString()),
        isDeveloped: this['is_developed'] as int == 1);
  }

  DbStateEntity toDbStateEntity() {
    return DbStateEntity(
      this['stt_id'].toString(),
      this['stt_name'].toString(),
      this['cou_id'].toString(),
      abbr: this['stt_abbr'].toString(),
      parentStateId: this['parent_state_id'].toString(),
    );
  }
}
